package com.example.tomato.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tomato.ChangePwd;
import com.example.tomato.FUser;
import com.example.tomato.R;
import com.example.tomato.User;
import com.example.tomato.friend.FriendInfo;
import com.example.tomato.tool.ServerHelper;

import java.util.ArrayList;
import java.util.List;

public class FriendRequestAdapter extends ArrayAdapter<FriendInfo> {
    private ArrayList<FUser> friendList;
    private ArrayList<FUser> searchRs=new ArrayList<>();
    private ArrayList<FUser> allUnknown=new ArrayList<>();
    private int resourceId;

    public FriendRequestAdapter(Context context, int resource, List<FriendInfo> objects) {
        super(context, resource, objects);
        resourceId = resource;
        friendList = new ArrayList<>();
        loadFriendRequestListData();
    }

    private void loadFriendRequestListData() {
        ServerHelper serverHelper = new ServerHelper();
        serverHelper.getUnknowUsers().thenAccept(complete->{
            if(complete!=null){
                allUnknown.clear();
                allUnknown.addAll((ArrayList<FUser>) complete);
                friendList=allUnknown;
                ((Activity) getContext()).runOnUiThread(this::notifyDataSetChanged);
            }
        });

    }

    public void filter(String query) {
        searchRs.clear();
        if (query.isEmpty()) {
            Log.i("search friend","null input");
            searchRs=allUnknown;
            Log.i("size of searchRs",String.valueOf(searchRs.size()));
            Log.i("size of allUnknown",String.valueOf(allUnknown.size()));
        } else {
            Log.i("search friend",query);
            for (FUser user : allUnknown) {
                if (user.getName().toLowerCase().contains(query.toLowerCase())) {
                    searchRs.add(user);
                }
            }

        }
        friendList=searchRs;
        ((Activity) getContext()).runOnUiThread(this::notifyDataSetChanged);
    }
    @Override
    public int getCount() {
        return friendList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            holder = new ViewHolder();
            holder.ivFriend = convertView.findViewById(R.id.icon);
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvEmail = convertView.findViewById(R.id.tv_email);
            holder.btnAdd=convertView.findViewById(R.id.btn_add);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.i("friendlist size",String.valueOf(friendList.size()));
        if (position >= 0 && position < friendList.size()) {
            FUser user = friendList.get(position);
            if (user != null) {
                FriendInfo friendInfo = getItem(position);
                holder.ivFriend.setImageResource(friendInfo.getIcon());
                holder.tvName.setText(user.getName());
                holder.tvEmail.setText(user.getEmail());

                holder.btnAdd.setOnClickListener(v->{
                    Log.i("add friend","click");
                    ServerHelper serverHelper=new ServerHelper();
                    serverHelper.addFriend(String.valueOf(user.getId())).thenAccept(complete->{
                        if(complete!=null){
                            Log.i("add friend","have result");
                            // 在主线程上显示 Toast
                            ((Activity) getContext()).runOnUiThread(() -> {
                                Toast.makeText(getContext(), "添加好友成功", Toast.LENGTH_SHORT).show();
                                friendList.remove(user);
                                notifyDataSetChanged();
                            });
                        } else {
                            // 在主线程上显示 Toast
                            ((Activity) getContext()).runOnUiThread(() ->
                                    Toast.makeText(getContext(), "添加好友失败", Toast.LENGTH_SHORT).show()
                            );
                        }
                    });
                });
            }
        }

        return convertView;
    }

    static class ViewHolder {
        Button btnAdd;
        ImageView ivFriend;
        TextView tvName;
        TextView tvEmail;
    }
}